package com.example.retrofit;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bean.Result;
import com.example.net.retrofit.IApi;
import com.example.net.retrofit.RetrofitImpl;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = findViewById(R.id.tv_content);
        findViewById(R.id.get_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IApi api = RetrofitImpl.getRetrofit().create(IApi.class);
                api.get("hi_man").enqueue(new Callback<Result>() {
                    @Override
                    public void onResponse(Call<Result> call, Response<Result> response) {
                        Result result = response.body();
                        mTextView.setText(result.errorCode
                                + "," + result.errorMsg + ","
                                + result.data.ip + "," + result.data.params
                                + "," + result.data.headers);
                    }

                    @Override
                    public void onFailure(Call<Result> call, Throwable throwable) {
                        Toast.makeText(MainActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        findViewById(R.id.post_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IApi api = RetrofitImpl.getRetrofit().create(IApi.class);
                api.post("hi_men123").enqueue(new Callback<Result>() {
                    @Override
                    public void onResponse(Call<Result> call, Response<Result> response) {
                        Result result = response.body();
                        mTextView.setText(result.errorCode + "," + result.errorMsg + "," + result.data.ip
                                + "," + result.data.params + "," + result.data.headers);

                    }

                    @Override
                    public void onFailure(Call<Result> call, Throwable throwable) {
                        Toast.makeText(MainActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        findViewById(R.id.post_multipart_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IApi api = RetrofitImpl.getRetrofit().create(IApi.class);
                MediaType mediaType = MediaType.get("text/plain;charset=utf-8");
                RequestBody username = RequestBody.create(mediaType,"hi_man_456");
                api.postMultipart(username).enqueue(new Callback<Result>() {
                    @Override
                    public void onResponse(Call<Result> call, Response<Result> response) {
                        Result result = response.body();
                        mTextView.setText(result.errorCode + "," + result.errorMsg + "," + result.data.ip
                                + "," + result.data.params + "," + result.data.headers);

                    }

                    @Override
                    public void onFailure(Call<Result> call, Throwable throwable) {
                        Toast.makeText(MainActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        findViewById(R.id.post_json_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IApi api = RetrofitImpl.getRetrofit().create(IApi.class);
                MediaType mediaType = MediaType.get("text/json;charset=utf-8");
                RequestBody username = RequestBody.create(mediaType,"{\"username\":\"hi_man_999\",\"age\":12}");
                api.postMultipart(username).enqueue(new Callback<Result>() {
                    @Override
                    public void onResponse(Call<Result> call, Response<Result> response) {
                        Result result = response.body();
                        mTextView.setText(result.errorCode + "," + result.errorMsg + "," + result.data.ip
                                + "," + result.data.params + "," + result.data.headers
                        +"," + result.data.json);

                    }

                    @Override
                    public void onFailure(Call<Result> call, Throwable throwable) {
                        Toast.makeText(MainActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

    }
}